package cn.sciento.farm.devicemanager.gateway.uplink.service.screen.impl;

import cn.sciento.farm.devicemanager.common.constant.CollectionTypeEnum;
import cn.sciento.farm.devicemanager.common.constant.DeviceConstant;
import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.common.util.DeviceUtil;
import cn.sciento.farm.devicemanager.common.util.NumberUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.model.CollectionInfo;
import cn.sciento.farm.devicemanager.dao.model.ScreenForm;
import cn.sciento.farm.devicemanager.gateway.downlink.service.impl.MqttByteSendService;
import cn.sciento.farm.devicemanager.gateway.downlink.service.impl.MqttSendService;
import cn.sciento.farm.devicemanager.gateway.model.downlink.MqttContent;
import cn.sciento.farm.devicemanager.gateway.uplink.service.screen.ScreenDisposeService;
import cn.sciento.farm.devicemanager.sensor.Collection;
import cn.sciento.farm.devicemanager.service.CollectionService;
import cn.sciento.farm.devicemanager.service.DeviceService;
import cn.sciento.farm.devicemanager.service.ScreenService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScreenDisposeServiceImpl implements ScreenDisposeService {

    @Autowired
    ScreenService screenService;

    @Autowired
    CollectionService collectionService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    MqttSendService mqttSendService;


    @Autowired
    MqttByteSendService mqttByteSendService;

    @Override
    public void trigger(String eui) throws UnsupportedEncodingException {
        ScreenForm screenForm = screenService.getByEui(eui);
        StringBuilder message = new StringBuilder("");
        String title = "";
        if(screenForm == null){

        }else {
            List<String> deviceIds = screenForm.getDeviceIds();
            for (String deviceId : deviceIds) {
                List<CollectionInfo> infoList = collectionService.getCollectionInfo(deviceId);
                DevDevice device = deviceService.getById(deviceId);
                if (device == null) {
                    continue;
                }
                String dtuName = String.format(DeviceConstant.SCREEN_DTU_TEMPLATE, device.getName());
                message.append(dtuName);
                for (CollectionInfo collectionInfo : infoList) {
                    CollectionTypeEnum typeEnum = CollectionTypeEnum.get(collectionInfo.getType());
                    String unit = typeEnum.getUnit();
                    String name = typeEnum.getName();
                    String value = getRealData(collectionInfo);
                    String sub = String.format(DeviceConstant.SCREEN_COLLECTION_TEMPLATE, name, value, unit);
                    message.append(sub);
                }
            }

            title = screenForm.getTitle();
        }

        Map map = new HashMap<String,String>();
        map.put("content",message.toString());
        map.put("title",title);
        String topic = String.format(MqttConstant.SCREEN_DOWN_ID, eui);
        String payload = JSON.toJSONString(map);
        mqttByteSendService.send(topic,payload.getBytes("gb2312"));
    }




    private String getRealData(CollectionInfo collectionInfo){
        float data = collectionInfo.getLastData();
        String readData = "";
        if(collectionInfo.getCollectionType().getId().equals(CollectionTypeEnum.WINDD.getType())){
            readData = DeviceUtil.handleWindDirData(data);
        }else if(collectionInfo.getCollectionType().getId().equals(CollectionTypeEnum.SLEET.getType())){
            if(data == 1){
                readData ="有";
            }else {
                readData = "无";
            }
        }else {
            float fData = NumberUtil.float2float(data,2);
            double proportion = collectionInfo.getCollectionType().getProportion();
            if(proportion >0 && proportion <=1 ){
                readData = (int)fData +"";
            }else {
                readData = fData +"";
            }
        }
        return readData;
    }
}
